using System.Linq;
using System.Collections.Generic;
using Managistics.Model;
using NHibernate.Criterion;

namespace Managistics.Core.Repositories.Impl
{
    public class InputRepository : RepositoryBase<InputSheet, string>, IInputRepository
    {
        /// <summary>
        /// select InputSheets by OrderId
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public List<InputSheet> GetInputSheetsOfOrderId(string orderId)
        {
            return this.Session.QueryOver<InputSheet>()
                                .Where(i => i.PurchaseOrder.Orderid == orderId)
                                .List<InputSheet>().ToList<InputSheet>();
        }

        /// <summary>
        /// select InputSheets
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public List<InputSheet> GetAllInputSheets()
        {
            return this.Session.QueryOver<InputSheet>()
                                .List<InputSheet>().ToList<InputSheet>();
        }

        /// <summary>
        /// Select maxId
        /// </summary>
        /// <returns></returns>
        public string SelectMaxId() 
        {
            InputSheet isheet = this.Session.QueryOver<InputSheet>()
                                .OrderBy(e => e.InputSheetId).Desc
                                .Take(1)
                                .SingleOrDefault();

            if (isheet != null)
            {
                return isheet.InputSheetId;
            }
            return null;
        }
    }
}